#include"BlockQueue.hpp"
#include"Task.hpp"
#include<ctime>
#include<pthread.h>

using namespace std;

int Add(int x,int y)
{
    return x + y;
}

void* productor(void* args)
{
    BlockQueue<Task>* bq=(BlockQueue<Task>*) args;
    
    int x=rand() % 10 + 1;
    int y=rand() % 5 + 1;
    Task t(x,y,Add);
    bq->push(t);
    cout<<"生产者:"<<t._x<<" + "<<t._y<<" = "<<endl;
}
void* consumer(void* args)
{
    BlockQueue<Task>* bq=new BlockQueue<Task>();
    Task t;
    bq->pop(&t);
    cout<<"消费者："<<t._x<<" + "<<t._y<<" = "<<t()<<endl;
}

int main()
{
    srand((uint64_t)time(nullptr) ^ getpid() ^ 0XFF45);
    BlockQueue<Task>* bq=new BlockQueue<Task>();

    pthread_t c[2],p[3];
    pthread_create(c,nullptr,consumer,(void*)bq);
    pthread_create(c+1,nullptr,consumer,(void*)bq);
    pthread_create(p,nullptr,productor,(void*)bq);
    pthread_create(p+1,nullptr,productor,(void*)bq);
    pthread_create(p+2,nullptr,productor,(void*)bq);

    for(int i=0;i<3;i++)
    {
        pthread_join(p[i],nullptr);
    }

    for(int i=0;i<2;i++)
    {
        pthread_join(c[i],nullptr);
    }
    delete bq;
    return 0;
}